﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using System.IO;

namespace CommonLibraries.Helpers
{
    public static class ExcelHelper
    {
        public static List<Bitmap> ConvertToImages(string filepath, string savedirectory)
        {
            try
            {
                var bmps = new List<Bitmap>();
                var book = new Workbook(filepath);
                foreach (Worksheet sheet in book.Worksheets)
                {
                    var imgOptions = new ImageOrPrintOptions();
                    imgOptions.ImageFormat = ImageFormat.Jpeg;
                    imgOptions.OnePagePerSheet = true;
                    imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
                    sheet.IsPageBreakPreview = true;

                    int cs = sheet.Cells.Columns.Count;
                    sheet.Cells.InsertColumn(cs);
                    for (int i = 0; i <= cs; i++)
                    {
                        var item = sheet.Cells.Columns[i];
                        if (i == cs)
                        {
                            item.Width = 1;

                            var scf1 = new Aspose.Cells.StyleFlag();
                            scf1.LeftBorder = false;
                            scf1.RightBorder = true;
                            scf1.TopBorder = true;
                            scf1.BottomBorder = true;

                            item.ApplyStyle(item.Style, scf1);
                        }
                    }

                    var sr = new SheetRender(sheet, imgOptions);
                    for (int i = 0; i < sr.PageCount; i++)
                    {
                        bmps.Add(sr.ToImage(i));
                    }
                }
                int count = 0;
                foreach (var item in bmps)
                {

                    item.Save(savedirectory + count + ".jpg");
                    count++;
                }
                return bmps;
            }
            catch (Exception e)
            {
                var bmps1 = new List<Bitmap>();
                var book1 = new Workbook(filepath);
                Directory.Delete(savedirectory);
                Directory.CreateDirectory(savedirectory);
                foreach (Worksheet sheet in book1.Worksheets)
                {
                    var imgOptions = new ImageOrPrintOptions();
                    imgOptions.ImageFormat = ImageFormat.Jpeg;
                    imgOptions.OnePagePerSheet = true;
                    imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
                    sheet.IsPageBreakPreview = true;
                    var sr = new SheetRender(sheet, imgOptions);
                    for (int i = 0; i < sr.PageCount; i++)
                    {
                        bmps1.Add(sr.ToImage(i));

                    }
                }
                int count = 0;
                foreach (var item in bmps1)
                {

                    item.Save(savedirectory + count + ".jpg");
                    count++;
                }
                return bmps1;
            }

        }

        public static List<Bitmap> ConvertToImagesLX(string filepath, string savedirectory)
        {

            var bmps1 = new List<Bitmap>();
            var book1 = new Workbook(filepath);
            //Directory.Delete(savedirectory);
            //Directory.CreateDirectory(savedirectory);
            foreach (Worksheet sheet in book1.Worksheets)
            {
                var imgOptions = new ImageOrPrintOptions();
                imgOptions.ImageFormat = ImageFormat.Jpeg;
                imgOptions.OnePagePerSheet = true;
                imgOptions.PrintingPage = PrintingPageType.IgnoreBlank;
                sheet.IsPageBreakPreview = true;
                var sr = new SheetRender(sheet, imgOptions);
                for (int i = 0; i < sr.PageCount; i++)
                {
                    bmps1.Add(sr.ToImage(i));

                }
            }
            int count = 0;
            foreach (var item in bmps1)
            {

                item.Save(savedirectory + count + ".jpg");
                count++;
            }
            return bmps1;
        }

    }
}